using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace MotaWeb.Controllers
{
    public class AnnouncementController : MotaControllerBase
    {
        //
        // GET: /Announcement/
        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            return View(Repository.FindAllAnnouncements());
        }

        //
        // GET: /Announcement/Details/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Details(int id)
        {
            return View(Repository.FindAnnouncement(id));
        }

        //
        // GET: /Announcement/Create
        [Authorize(Roles = "Administrator")]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Announcement/Create
        [Authorize(Roles = "Administrator")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                string message = collection["Message"];
                DateTime effectiveDate = DateTime.Parse(collection["EffectiveDate"]);
                DateTime expirationDate = DateTime.Parse(collection["ExpirationDate"]);
                Repository.CreateAnnouncement(message, effectiveDate, expirationDate);

                return RedirectToAction("Index");
            }
            catch(Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
                return View();
            }
        }

        //
        // GET: /Announcement/Edit/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id)
        {
            return View(Repository.FindAnnouncement(id));
        }

        //
        // POST: /Announcement/Edit/5

        [Authorize(Roles = "Administrator")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }
            try
            {
                string message = collection["Message"];
                DateTime effectiveDate = DateTime.Parse(collection["EffectiveDate"]);
                DateTime expirationDate = DateTime.Parse(collection["ExpirationDate"]);
                Repository.EditAnnouncement(id, message, effectiveDate, expirationDate);

                return RedirectToAction("Index");
            }
            catch(Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
                return View();
            }
        }

        //
        // POST: /Announcement/Delete/5

        [Authorize(Roles = "Administrator")]
        [AcceptVerbs(HttpVerbs.Delete)]
        public ActionResult Delete(int id)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }
            try
            {
                Repository.DeleteAnnouncement(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
